package thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-29
 * Time: 14:15
 */
public class ThreadDemon36 {

   // private static int result = 0;
    //括号里的参数就是result的初始值
    private static AtomicInteger result = new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
           for(int i = 1 ; i <= 5000 ; i++){
               result.getAndIncrement();//自增相当于result++
               //result++;

           }
        });
        Thread t2 = new Thread(() -> {
            for(int i = 1 ; i <= 5000 ; i++){
                result.getAndIncrement();//自增
                //result++;
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(result.get());//获取到result中持有的值
    }
}
